package controller;


import model.Item;
import model.Player;

import java.io.BufferedWriter;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;

public class DatabaseController {
    private static Connection conn;
    public static void setConnection(Connection conn) {
        DatabaseController.conn = conn;
    }

    public static int getTotal() throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery("select count(*) from player");
        rs.next();
        return rs.getInt(1);
    }

    public static Player check(String id, String pwd, BufferedWriter bw) throws SQLException, IOException{
        Statement stmt = conn.createStatement();
        String sql="SELECT player.player_id,player_name,player_experience,con,dex,str,wis,max_hp,hp,max_nl,nl,max_jl,jl,location,flag"
                + " FROM login,player WHERE login.player_id=player.player_id AND login.player_id='"+id +"' AND login.pwd='"+pwd+"';";
        ResultSet rs = stmt.executeQuery(sql);
        if(!rs.next()){
            bw.write("您的输入有误,请重新输入");
            bw.newLine();
            bw.flush();
            return null;
        }
        if(rs.getInt("flag") == 1){
            bw.write("您已在游戏内，请勿重复登录");
            bw.newLine();
            bw.flush();
            return null;
        }

        String player_id = rs.getString("player_id");
        String player_name = rs.getString("player_name");
        int experience = rs.getInt("player_experience");
        int con = rs.getInt("con");
        int dex = rs.getInt("dex");
        int str = rs.getInt("str");
        int wis = rs.getInt("wis");
        int max_hp = rs.getInt("max_hp");
        int hp = rs.getInt("hp");
        int max_nl = rs.getInt("max_nl");
        int nl = rs.getInt("nl");
        int max_jl = rs.getInt("max_jl");
        int jl = rs.getInt("jl");
        String location = rs.getString("location");
        stmt.execute("UPDATE player SET flag=1 WHERE player_id='"+id+"';");
        bw.write("登录成功");
        bw.newLine();
        bw.flush();
        Player p= new Player(experience,con,dex,str,wis,hp,max_hp,nl,max_nl,jl,max_jl,player_id,player_name,location,1);
        HashMap<String,Item> b=p.getBag();
        ResultSet r =stmt.executeQuery("SELECT item,num FROM bag WHERE player_id='"+player_id+"';");
        while (r.next()){
            Item item=ItemController.getItem(r.getString("item"));
            item.setNum(r.getInt("num"));
            b.put(item.getName(),item);
        }
        stmt.execute("DELETE FROM bag WHERE player_id='"+player_id+"';");
        return p;
    }

    public static boolean addnewPlayer(Player p, String pwd) throws SQLException {
        Statement stmt = conn.createStatement();
        ResultSet rs=stmt.executeQuery("select count(*) from player where player_name='"+p.getName()+"';");
        rs.next();
        if(rs.getInt(1)>0){
            return false;
        }else {
            String sql="INSERT INTO player(player_id,player_name,player_experience,con,dex,str,wis,max_hp,hp,max_nl,nl,max_jl,jl,location,flag) " +
                    "VALUES ('"+p.getId()+"','"+p.getName()+"',"+p.getExperience()+","+p.getCon()+","+p.getDex()+","+p.getStr()+","+p.getWis()+","
                    +p.getMax_hp()+","+ p.getHp()+","+p.getMax_nl()+","+p.getNl()+","+p.getMax_jl()+","+p.getJl()+",'"+p.getLocation()+"',1);";
            stmt.execute(sql);
            stmt.execute("INSERT INTO login(player_id,pwd) VALUES ('"+p.getId()+"','"+pwd+"');");
            return true;
        }
    }
    public static void updatePlayer(Player p) throws SQLException {
        Statement stmt = conn.createStatement();
        String sql="UPDATE player SET player_experience="+p.getExperience()+",con="+p.getCon()+",dex="+p.getDex()+",str="+p.getStr()+",wis="+p.getWis()+
                ",max_hp="+p.getMax_hp()+",hp="+p.getHp()+",max_nl="+p.getMax_nl()+",nl="+p.getNl()+",max_jl="+p.getMax_jl()+",jl="+
                p.getJl()+",location='"+p.getLocation()+
                "',flag=0 WHERE player_id='"+p.getId()+"';";
        stmt.execute(sql);
        for(Item item:p.getBag().values()){
            stmt.execute("INSERT INTO bag(player_id,item,num) VALUES ('"+p.getId()+"','"+item.getName()+"',"+item.getNum()+");");
        }
    }
}
